﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Cookie_Hero.graphics
{
    public class TextureArray
    {
        // List of textures to load
        List<Texture2D> textures;
        // Name of image sequence
        public String name { get; set; }
        // Id of the image sequence (must be identical)
        public int id { get; set; }
        // Selected image in animation
        public int currentFrame { get; set; }
        // Indicates if the animation is playing
        public bool playing { get; set; }
        // Indicates if the animation exists of one image
        private bool singleImage;

        /* Create a texture array */
        public TextureArray(int id, List<Texture2D> textures)
        {
            this.id = id;
            this.textures = textures;
            this.currentFrame = 0;
            this.playing = true;
            singleImage = (textures.Count <= 1);
        }

        /* Update the frame of the animation */
        public void update()
        {
            if (playing && !singleImage)
            {
                currentFrame++;
                if (currentFrame >= textures.Count)
                {
                    currentFrame = 0;
                }
            }
        }

        /* Return the Texture of a frame in the collection */
        public Texture2D getTextureByFrame(int frame)
        {
            if(textures == null)
            {
                return null;
            }
            if (singleImage)
            {
                return textures[0];
            }
            if (frame >= textures.Count)
            {
                Log.W("Texture not availible, frame "+frame+" from "+name);
                return textures[textures.Count];
            }
            return textures[frame];
        }

        /* Return the current Texture in the animation */
        public Texture2D getTexture()
        {
            return getTextureByFrame(currentFrame);
        }

    }
}
